﻿namespace CodeBetter.Extensions.Tests
{
   using System.Collections.Generic;
   using NUnit.Framework;

   [TestFixture]
   public class IDictionaryTests
   {
      [Test]
      public void ContainsValue_ReturnsFalseIfValueDoesntExist()
      {
         var dictionary1 = new Dictionary<string, int>();
         Assert.IsFalse(dictionary1.ContainsValue(3));
         Assert.IsFalse(dictionary1.ContainsValue(0));

         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         Assert.IsFalse(dictionary2.ContainsValue(3));
         Assert.IsFalse(dictionary2.ContainsValue(0));
      }
      [Test]
      public void ContainsValue_ReturnsTrueIfValueDoesntExist()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         Assert.IsTrue(dictionary2.ContainsValue(1));
         Assert.IsTrue(dictionary2.ContainsValue(2));
      }

      [Test]
      public void TryGetKey_ReturnsFalseAndDefaultIfValueNotFound()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         string key;
         Assert.IsFalse(dictionary2.TryGetKey(4, out key));
         Assert.IsNull(key);
      }
      [Test]
      public void TryGetKey_ReturnsTrueAndKeyIfValueFound()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         string key;
         Assert.IsTrue(dictionary2.TryGetKey(1, out key));
         Assert.AreEqual("a", key);
      }
      [Test]
      public void TryGetKey_ReturnsFirstKey()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 }, {"c", 1} };
         string key;
         Assert.IsTrue(dictionary2.TryGetKey(1, out key));
         Assert.AreEqual("a", key);
      }


      [Test]
      public void TryGetKeys_ReturnsFalseAndDefaultIfValueNotFound()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         string[] keys;
         Assert.IsFalse(dictionary2.TryGetKeys(4, out keys));
         Assert.AreEqual(0, keys.Length);
      }
      [Test]
      public void TryGetKeys_ReturnsTrueAndKeysIfValueFound()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
         string[] keys;
         Assert.IsTrue(dictionary2.TryGetKeys(1, out keys));
         Assert.AreEqual(1, keys.Length);
         Assert.AreEqual("a", keys[0]);
      }
      [Test]
      public void TryGetKeys_ReturnsAllKeys()
      {
         var dictionary2 = new Dictionary<string, int> { { "a", 1 }, { "b", 2 }, { "c", 1 } };
         string[] keys;
         Assert.IsTrue(dictionary2.TryGetKeys(1, out keys));
         Assert.AreEqual(2, keys.Length);
         Assert.AreEqual("a", keys[0]);
         Assert.AreEqual("c", keys[1]);
      }
   }

}
